<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\FhQuestion;
use App\Models\FhTag;
use App\Models\FhCategory;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class FhQuestionController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new FhQuestion(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->model()->orderBy('id','desc');
            $grid->column('title');
            $grid->column('img')->image('',100,40);
            $grid->column('q_desc')->width(500);
            $grid->column('pid')->display(function ($p_id) {
                $title = FhCategory::where('id', $p_id)->value('c_name');
                return $title==""?"顶级":$title;
            });
            $grid->column('show')->switch();
            $grid->column('sort')->editable();
            $grid->column('created_at');
            $grid->filter(function (Grid\Filter $filter) {
                $filter->like('title');
        
            });
            $grid->actions(function ($actions) {
                // 隐藏显示按钮
                $actions->disableView();
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new FhQuestion(), function (Show $show) {
            $show->field('id');
            $show->field('title');
            $show->field('q_desc');
            $show->field('content');
            $show->field('show');
            $show->field('img');
            $show->filed('pid');
            $show->filed('tag');
            $show->filed('thumb');
            $show->field('sort');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new FhQuestion(), function (Form $form) {
            $form->display('id');
            $form->text('title')->required();
            $form->text('url',"路由地址")->required();
            $form->text('q_desc')->maxLength(191)->required();
            $form->select("pid")->options("/apis/top")->required();
            $form->multipleSelect('tag',"标签")->options('/apis/getTag')->saving(function ($value) {
            // 转化成json字符串保存到数据库
                return implode(',',$value);
            });
            $form->editor('content')->required();
            $form->hidden('thumb');
            $form->switch('show')->default(1);
            $form->image('img')->thumbnail('small', $width = 300, $height = 300)->move('images/'.date("Ymd",time()));
            $form->number('sort')->min(1)->default(1);
            $form->saving(function (Form $form) {
                $form->model()->id; 
                if (!$form->model()->id) {
                    $pattern = '/[ ?\&.\{\}#\s]/';  // 正则表达式
                    $isValid = preg_match($pattern,  $form->url) === 1; // 验证逻辑
                    if($isValid){
                        throw new \Exception('路由地址包含/ ?&.{}# 空格特殊字符');
                    }
                }
                if($form->img){
                    $thumb=explode(".",$form->img);
                    $form->thumb=$thumb[0].'-small.'.$thumb[1];
                }
            });
            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
